package com.quick.im.client.factory;

import com.google.common.collect.Maps;
import com.quick.im.client.interfaces.MessageStrategy;
import lombok.Setter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import java.util.Map;



@Configuration
public class StrategyBeanFactory implements InitializingBean {

    @Setter(onMethod_ = @Autowired)
    private ApplicationContext applicationContext;

    private static Map<String, MessageStrategy> messageStrategyMap = Maps.newHashMap();

    public MessageStrategy getMessageServerByName(String name){
        MessageStrategy messageStrategy = messageStrategyMap.get(name);
        if (messageStrategy == null){
            return null;
        }
        return messageStrategy;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, MessageStrategy> beansOfType = applicationContext.getBeansOfType(MessageStrategy.class);
        messageStrategyMap.putAll(beansOfType);
    }
}
